using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Xml.Serialization;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.Office.Workflow.Utility;


namespace MailAnnouncementToRecipients
{
	public sealed partial class Workflow1: SequentialWorkflowActivity
	{
        private SPList emailRecipients;
        private int numberRecipients;
        private int currentRecipient;
        private string listName;
        private string kolom;
        private string urlWeb;

		public Workflow1()
		{
			InitializeComponent();
		}

        public Guid workflowId = default(System.Guid);
        public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties workflowProperties = new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();

        private void CheckRecipients(object sender, ConditionalEventArgs e)
        {
            if (currentRecipient < numberRecipients)
            {
                sendEmail1_To = emailRecipients.Items[currentRecipient][kolom].ToString().Split('#')[1];
                currentRecipient++;
                e.Result = true;
            }
            else
                e.Result = false;           
        }
        
        public String sendEmail1_To = default(System.String);
        public String sendEmail1_Subject = default(System.String);
        public String sendEmail1_Body = default(System.String);

        private void sendEmail1_MethodInvoking(object sender, EventArgs e)
        {
            sendEmail1.Subject = "New announcement in list: " + workflowProperties.Item["Title"].ToString();
            sendEmail1.Body = workflowProperties.Item["Body"].ToString();
        }

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            System.Diagnostics.EventLog.WriteEntry("test", faultHandlerActivity1.Fault.Message.ToString());
        }

        public String logToHistoryListActivity_HistoryDescription = default(System.String);
        public String logToHistoryListActivity_HistoryOutcome = default(System.String);

        private void logToHistoryListActivity1_MethodInvoking(object sender, EventArgs e)
        {
            logToHistoryListActivity_HistoryDescription = "Workflow started";
            logToHistoryListActivity_HistoryOutcome = "Outcome: Going to send " + numberRecipients + "emails";
        }

        private void logToHistoryListActivity2_MethodInvoking(object sender, EventArgs e)
        {
            logToHistoryListActivity_HistoryDescription = "Workflow in progress";
            logToHistoryListActivity_HistoryOutcome = "Outcome: send email to: " + sendEmail1_To;
        }

        private void logToHistoryListActivity3_MethodInvoking(object sender, EventArgs e)
        {
            logToHistoryListActivity_HistoryDescription = "Workflow ended";
            logToHistoryListActivity_HistoryOutcome = "Outcome: Workflow ended";
        }

        private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(workflowProperties.AssociationData);
            
            urlWeb = xmldoc.DocumentElement.ChildNodes[0].InnerText;
            kolom = xmldoc.DocumentElement.ChildNodes[1].InnerText;
            listName = xmldoc.DocumentElement.ChildNodes[2].InnerText;

            SPWeb thisWeb = new SPSite(urlWeb).OpenWeb();
            emailRecipients = thisWeb.Lists[listName];
            numberRecipients = emailRecipients.ItemCount;
            currentRecipient = 0;
        }
	}

}
